 /***************************************************************************
 *   Copyright (C) 2010 by ZhangShine , All Rights Reserved                                  
 *   zhangshine0125@gmail.com                                              
 *                                                                         
 *   This program is free software; you can redistribute it and/or modify  
 *   it under the terms of the GNU General Public License as published by  
 *   the Free Software Foundation; either version 2 of the License, or     
 *   (at your option) any later version.                                   
 *                                                                         
 *   This program is distributed in the hope that it will be useful,       
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 *   GNU General Public License for more details.                          
 *                                                                         
 *   You should have received a copy of the GNU General Public License     
 *   along with this program; if not, write to the                         
 *   Free Software Foundation, Inc.,                                       
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             
 ***************************************************************************/

#include "notifer.h"
#include "ui_notifer.h"
#include "QLisaGlobal.h"
#include <QtCore>
#include <QtGui>

/*! \brief Notifer类构造函数
 *
 * 设置提醒框的样式
 */
Notifer::Notifer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Notifer)
{
    ui->setupUi(this);
    ui->frame_MainFrame->installEventFilter(this);
    ui->frame_MainFrame->setMouseTracking(true);
    this->currentMessageNumber = 0;
    timer = new QTimer(this);
    http = new Http(this);
    this->setWindowFlags(Qt::Tool|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);// frameless
    /* hide background */
    setAttribute(Qt::WA_TranslucentBackground, true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor( 0, 0, 0, 0));
    setPalette(pal);
    
    this->hide();
    this->timePassby = 0.0;
    
    QObject::connect(ui->pushButton_Pre, SIGNAL(clicked()), this, SLOT(PreButtonClicked()));
    QObject::connect(ui->pushButton_Next, SIGNAL(clicked()), this, SLOT(NextButtonClicked()));
    QObject::connect(ui->pushButton_Exit, SIGNAL(clicked()), this, SLOT(ExitButtonClicked()));
}

Notifer::~Notifer()
{
    qDebug("notifer deleting!");
    delete ui;
}

/*! \brief 设置多条提醒信息并显示
 *
 * 用来显示微博提醒信息
 */
void Notifer::setNotiferContentAndShow(QList<QLisa::NotifyObject> messageList)
{
    if(messageList.isEmpty())
        this->close();
    this->mList = messageList;
    if(mList.length()==1)
    {
        ui->pushButton_Pre->setEnabled(false);
        ui->pushButton_Next->setEnabled(false);
        ui->label_Progress->setText("1/1");
    }
    else {
        ui->pushButton_Pre->setEnabled(false);;
        ui->label_Progress->setText("1/"+QString::number(mList.length()));
        qDebug("mlist length is %d", mList.length());
    }
    currentMessageNumber=0;
    downloadPixmap();
    setText();
    showIt();
}

/*! \brief 设置单条提醒信息并显示
 *
 * 可用来显示提示信息(未实现)
 */
void Notifer::setNotiferContentAndShow(QString msg)
{
    ui->pushButton_Pre->setEnabled(false);
    ui->pushButton_Next->setEnabled(false);
    ui->label_Progress->setText("1/1");
    setText(msg);
    showIt();
}

/*! \brief 显示提醒消息
 *
 */
void Notifer::showIt()
{
    QDesktopWidget *desktop = QApplication::desktop();
    QRect rect = desktop->screenGeometry(this);
    this->setGeometry( rect.width()-width(), rect.height()-height()-40, width(), height());
    this->show();
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeOutDeal()));
    timer->start(1000);//1s
}

/*! \brief 定时器的槽，用来定时渐隐
  */
void Notifer::timeOutDeal()
{
    timePassby++;
    if(timePassby>10)
        fadeOut();
}

/*! \brief 渐隐
  */
void Notifer::fadeOut()
{
    if(windowOpacity()<0.1)
        this->close();
    else
    {
        setWindowOpacity(1.0-(timePassby-10)*0.2);
    }
}

/*! \brief 鼠标事件处理
 *
 * 当鼠标进入提醒框时，取消自动隐藏，鼠标离开提醒框时，开始计时渐隐
 */
bool Notifer::eventFilter(QObject *target, QEvent *event)
{
    if(target==ui->frame_MainFrame)
    {
        switch(event->type())
        {
        case QEvent::MouseMove:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            return true;
            break;
        case QEvent::Enter:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            timer->stop();
            return true;
            break;
        case QEvent::Leave:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            timer->start();
            return true;
            break;
        default:
            break;
        }
    }    
    return false;
}

/*! \brief 显示上一条提醒消息
  */
void Notifer::PreButtonClicked()
{
    currentMessageNumber--;
    if(currentMessageNumber<=0)
    {
        currentMessageNumber = 0;
        ui->pushButton_Pre->setEnabled(false);
    }
    ui->pushButton_Next->setEnabled(true);
    ui->label_Progress->setText(QString::number(currentMessageNumber+1)+"/"+QString::number(mList.length()));
    setText();
}

/*! \brief 显示下一条提醒消息
  */
void Notifer::NextButtonClicked()
{
    currentMessageNumber++;
    if(currentMessageNumber>=(mList.length()-1))
    {
        currentMessageNumber = mList.length()-1;
        ui->pushButton_Next->setEnabled(false);
    }
    ui->pushButton_Pre->setEnabled(true);
    ui->label_Progress->setText(QString::number(currentMessageNumber+1)+"/"+QString::number(mList.length()));
    setText();
}

/*! \brief 关闭
 */
void Notifer::ExitButtonClicked()
{
    this->close();
}
/*! \brief 设置提醒内容
 *
 *  默认为要显示的提醒消息
 */
void Notifer::setText()
{
    if(currentMessageNumber>=0&&currentMessageNumber<mList.length())
    {   
        QPixmap pTmp;
        Status status = mList.at(currentMessageNumber).status;
        ui->label_StatusText->setText(status.getUserName()+" : \n"+status.getText());
        ui->label_StatusText->adjustSize();
        if(!status.getThumbnailPic().isEmpty())//微博有图片
        {
            pTmp = this->urlPixmapMap.value(status.getThumbnailPic());
            ui->label_StatusImage->setPixmap(pTmp);
            //ui->label_StatusImage->resize(pTmp.width(), pTmp.height());
            ui->label_StatusImage->setMinimumHeight(pTmp.height());
        }
        else //微博不含图片
        {
            ui->label_StatusImage->hide();
        }
        if(!status.getRetweetedId().isEmpty())//有转发的微博
        {
            Status retwStatus = mList.at(currentMessageNumber).retwStatus;
            ui->label_RetwStatusText->setText(retwStatus.getUserName() + " : \n" + retwStatus.getText());
            ui->label_RetwStatusText->adjustSize();
            if(!retwStatus.getThumbnailPic().isEmpty())//转发的微博含有图片
            {
                pTmp = urlPixmapMap.value(retwStatus.getThumbnailPic());
                ui->label_RetwStatusImage->setPixmap(pTmp);
                //ui->label_RetwStatusImage->resize(pTmp.width(), pTmp.height());
                ui->label_RetwStatusImage->setMinimumHeight(pTmp.height());
            }
            else
            {
                ui->label_RetwStatusImage->hide();
            }
        }
        else //没有转发的微博
        {
            ui->frame_retwFrame->hide();
        }
    }
    ui->frame_StatusFrame->adjustSize();
    ui->frame_retwFrame->adjustSize();
    ui->frame_MainFrame->adjustSize();
    this->adjustSize();
}

/*! \brief 显示提示信息
 *
 * 不用来显示微博，可以用来显示一条提示信息，目前还未实现
 */
void Notifer::setText(QString msg)
{
    
}

/*! \brief 下载图片
 *
 * 若微博含有图片，则下载图片
 */
void Notifer::downloadPixmap()
{
    if(currentMessageNumber>=0&&currentMessageNumber<mList.length())
    {
        Status pStatus, pRetwStatus;
        Http http; QNetworkRequest request;
        QPixmap pixmap;
        for(int i=0; i<mList.length(); i++)
        {
                pStatus = mList.at(i).status;
                if(!pStatus.getThumbnailPic().isEmpty())//微博含有图片,则没有转发的微博
                {
                    request.setUrl(QUrl(pStatus.getThumbnailPic()));
                    pixmap = http.getImageFromUrl(request);
                    this->urlPixmapMap.insert(pStatus.getThumbnailPic(), pixmap);
                }
                else //转发的微博是否有图片
                {
                    if(!pStatus.getRetweetedId().isEmpty())//有转发的微博
                    {
                        pRetwStatus = mList.at(i).retwStatus;
                        if(!pRetwStatus.getThumbnailPic().isEmpty())//转发的微博有图片
                        {
                            request.setUrl(QUrl(pRetwStatus.getThumbnailPic()));
                            pixmap = http.getImageFromUrl(request);
                            this->urlPixmapMap.insert(pRetwStatus.getThumbnailPic(), pixmap);
                        }
                    }
                }
        }
    }
}
